package com.kucun.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kucun.bean.Units;
import com.kucun.bean.Page;
import com.kucun.bean.Users;
import com.kucun.service.UnitsService;
import com.kucun.util.UID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
@RequestMapping("/units")
public class UnitsController {

	@Autowired
	private UnitsService unitsservice;
	
	/**
	 * 
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getallunits.html")
	public String getAllUser(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
	       String field=request.getParameter("field");
	        String searchertext=request.getParameter("searchertext");
	        if("".equals(searchertext)||searchertext==null){
				 
	        }else{
	        	searchertext = URLDecoder.decode(searchertext, "utf-8");
	        } 
			Page page1=new Page();
			page1.setI(rows*(page-1));
			page1.setRows(rows);
			page1.setFiled(field);
			page1.setSearchertext(searchertext);
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		List<Units> list = unitsservice.findAll(page1);	
		jsonMap.put("total",unitsservice.getCount().getTotal());//total键 存放总记录数，必须的  
        jsonMap.put("rows",list);//rows键 存放每页记录 list  
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	
	/**
	 * @param request
	 * @return
	 */
	@RequestMapping("/toaddunits.html")
	public String toAddUser(HttpServletRequest request){
		
		return "/addUser";
	}
	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/addunits.html")
	public String addUnits(Units units,HttpServletRequest request,HttpServletResponse response) throws IOException{
		//Users user=(Users)request.getSession().getAttribute("user");
		String tag=request.getParameter("tag");
		int s=0;
		PrintWriter pw = response.getWriter();
		if("1".equals(tag)){
			int ss=unitsservice.updateByPrimaryKeySelective(units);
			if(ss>0){
				 pw.write("ok");
			}else{
				 pw.write("err");
			}
		}else{
			//String billon="WP"+UID.next();
			units.setId(null);
			s=unitsservice.save(units);
			if(s>0){
				 pw.write("ok");
			}else{
				 pw.write("err");
			}
		}
		return null;
	}
	
	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/updateunits.html")
	public String updateUser(String Unitsname,Units user,HttpServletRequest request,HttpServletResponse response) throws IOException{
		int s=unitsservice.updateGoodsForDel(Unitsname);
		PrintWriter out = response.getWriter();
		if(s>0){
			 out.write("ok");
		}else{
			 out.write("err");
		}
		return null;
	}
	/**
	 * 查询所有数据
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/getallunitsnopaging.html")
	public String getAllDateNoPaging(HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Units> list = unitsservice.findAllDateNoPaging();	
        JSONArray jsonObject = JSONArray.fromObject(list);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	
	
}
